package com.example.emqx.controller;

import com.example.emqx.entity.DevicesEmqAll;
import com.example.emqx.entity.DevicesEmqPayload;
import com.example.emqx.service.DevicesEmqAllService;
import com.example.emqx.service.DevicesEmqPayloadService;
import com.example.emqx.util.EmqUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * File: AuthController.java
 * Author: liufudong@szyh-smart.com
 * Date: 2024/8/29
 * Description: AuthController class.
 * 文档在https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html
 */
@RestController
@RequestMapping("/mqtt")
@Slf4j
public class WebhookController {
    //http://192.168.0.103:9000/mqtt/webhook
    //http://192.168.0.103:9000/mqtt/getall
    @Resource
    private ObjectMapper objectMapper;
    private Map<String, Boolean> clientStatusMap = new HashMap<>();

    @Autowired
    private DevicesEmqAllService devicesAllEmqService;

    @Autowired
    private DevicesEmqPayloadService devicesEmqPayloadService;

    @PostConstruct
    public void init() {
    }

    @PostMapping(path = "/webhook")
    public void postMsg(
            @RequestBody Map<String, Object> map
    ) {
        try {
            log.info("webhook,请求体数据=:{}", objectMapper.writeValueAsString(map));
            saveDevicesEmq(map);
        } catch (JsonProcessingException e) {
            log.info("JsonProcessingException:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    public void saveDevicesEmq(Map<String, Object> map)  {
        DevicesEmqAll devicesEmqAll = EmqUtil.getDevicesEmq(map);
        boolean save = devicesAllEmqService.save(devicesEmqAll);
        log.info("WebhookController saveDevicesEmq:{}", save);
        String event = (String) map.get("event");
        String clientId = (String) map.get("clientid");
        log.info("event:{}", event);
        if (event == null) {
            return;
        }
        if (event.equals("client.connected")) {
            //客户端成功接入
//            clientStatusMap.put(clientId, true);
        } else if (event.equals("client.disconnected")) {
            //客户端断开连接
//            clientStatusMap.put(clientId, false);
        } else if (event.equals("message.publish")) {
            //发送消息
            DevicesEmqPayload devicesEmqPayload = null;
            try {
                devicesEmqPayload = EmqUtil.jsonToDevicesEmqPayload(objectMapper,objectMapper.writeValueAsString(map));
                devicesEmqPayloadService.save(devicesEmqPayload);
                log.info("WebhookController saveDevicesEmqPayload:{}", save);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }


    @GetMapping("/getall")
    public String getAllStatus() {
        try {
            return objectMapper.writeValueAsString(clientStatusMap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
